#ifndef _SY880X_H_
#define _SY880X_H_

#include "main.h"
//#include "headset_cmd.h"
#include <stdbool.h>


//I2C address
#define APP_SY8801_IIC_WRITE_ADDR 0x0C
#define APP_SY8801_IIC_READ_ADDR  0x0D
//Register addr
//status
#define ADDR_sy8801_ST_ERR      0x10
#define ADDR_sy8801_ST_LOAD     0x11
#define ADDR_sy8801_ST_BAT      0x12
#define ADDR_sy8801_ST_INIT     0x13
//configure
#define ADDR_sy8801_CONF_P06    0x06
#define ADDR_sy8801_CONF_P20    0x20
#define ADDR_sy8801_CONF_P21    0x21
#define ADDR_sy8801_CONF_P40    0x40
#define ADDR_sy8801_CONF_P41    0x41
#define ADDR_sy8801_CONF_P42    0x42
#define ADDR_sy8801_CONF_P43    0x43
#define ADDR_sy8801_CONF_P45    0x45
#define ADDR_sy8801_CONF_P46    0x46
//control
#define ADDR_sy8801_CTRL_COMIN  0x30
#define ADDR_sy8801_CTRL_OUT    0x31
#define ADDR_sy8801_CTRL_LED    0x50
//counts
#define COUNT_SY8801_REPEAT     5   //(50ms+70ms) * COUNT_SY8801_REPEAT = ?ms
#define COUNT_SY8801_EAR_CHARGE_DONE     1   //(50ms+70ms) * COUNT_SY8801_REPEAT = ?ms

#define ENABLE_BAT_CHARGE_CTRL()  
#define DISABLE_BAT_CHARGE_CTRL()  
#define ENABLE_BAT_CHARGE_CTRL_HALFV() 
typedef enum 
{
    BIT_sy8801_ST_ERR_INTERNAL = 0,
    BIT_sy8801_ST_ERR_HIGH_L = 1,
    BIT_sy8801_ST_ERR_HIGH_R = 2,
    BIT_sy8801_ST_ERR_BAT_NORMAL = 3,
    
    BIT_sy8801_ST_ERR_VIN_NORMAL = 4,
    BIT_sy8801_ST_ERR_CHRAGING = 5,
    BIT_sy8801_ST_ERR_CHG_END = 6,
    BIT_sy8801_ST_ERR_BOOST = 7,
}BIT_sy8801_ST_ERR;

typedef enum 
{
    BIT_sy8801_CTRL_OUT_CHARGE = 0,
    BIT_sy8801_CTRL_OUT_BOOST = 1,
    BIT_sy8801_CTRL_OUT_VOL = 2,
    BIT_sy8801_CTRL_OUT_VOR = 3
}BIT_sy8801_CTRL_OUT;

typedef enum 
{
    BIT_sy8801_ST_LOAD_INTrig_L = 0,
    BIT_sy8801_ST_LOAD_INTrig_R = 1,
    BIT_sy8801_ST_LOAD_IN_L = 2,
    BIT_sy8801_ST_LOAD_IN_R = 3,
    BIT_sy8801_ST_LOAD_LOW_L = 4,
    BIT_sy8801_ST_LOAD_LOW_R = 5,
    BIT_sy8801_ST_LOAD_NORMAL_L = 6,
    BIT_sy8801_ST_LOAD_NORMAL_R = 7
    
}BIT_sy8801_ST_LOAD;

typedef struct {
    uint8_t init;
    uint8_t err;
    uint8_t load;
    uint8_t bat_reg;
    uint8_t led;
    uint8_t out;
} sy8801_struct_t;

extern sy8801_struct_t sy8801;

void sy880x_write_reg(uint8_t reg, uint8_t dat);
uint8_t sy880x_read_reg(uint8_t reg);
void sy880x_init(void);
uint8_t get_sy800x_is_ready(void);


uint16_t app_get_sy8801_battery(void);

void sy8801_check_abnormal(void);
void sy8801_check_usb_state(uint8_t new_err);
void sy8801_check_ear_state(uint8_t new_load);

void sy8801_ear_5v_output_enable(uint8_t role);
void sy8801_ear_5v_output_disable(uint8_t role);

void sy8801_auto_detect_enable(void);
void sy8801_auto_detect_disable(void);

void app_sy880x_heartbeat_timer_start(void);
void app_sy880x_heartbeat_timer_stop(void);
void app_sy880x_heartbeat_handle(void);
void app_sy880x_state_handle(void);

void app_sy880x_enter_sleep(void);

void app_sy880x_set_led(uint8_t level);

#endif
